package com.sherllinhe.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.sherllinhe.common.context.LoginMemberContext;
import com.sherllinhe.business.domain.Train;
import com.sherllinhe.business.domain.TrainExample;
import com.sherllinhe.business.resp.TrainQueryResp;
import com.sherllinhe.common.exception.BusinessException;
import com.sherllinhe.common.exception.BusinessExceptionEnum;
import com.sherllinhe.common.resp.PageResp;
import com.sherllinhe.common.util.SnowUtil;
import com.sherllinhe.business.domain.Station;
import com.sherllinhe.business.domain.StationExample;
import com.sherllinhe.business.mapper.StationMapper;
import com.sherllinhe.business.req.StationQueryReq;
import com.sherllinhe.business.req.StationSaveReq;
import com.sherllinhe.business.resp.StationQueryResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：
 * 作者：Sherllin He
 * 日期：2025/5/7 16:48
 */
@Slf4j
@Service
public class StationService {
    @Autowired
    private StationMapper stationMapper;
    // 新增和编辑乘客
    public void save(StationSaveReq req){
        DateTime now = DateTime.now();
        Station station = BeanUtil.copyProperties(req, Station.class);

        //判断是新增还是更新
        //station.setMemberId(LoginMemberContext.getId());
        if(ObjUtil.isNull(station.getId())) {
            //保存前，校验唯一键是否存在
            Station stationDB = selectByUnique(req.getName());
            if(ObjUtil.isNotEmpty(stationDB)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
            }
            //新增
            station.setId(SnowUtil.getSnowflakeNextId());
            station.setCreateTime(now);
            station.setUpdateTime(now);
            stationMapper.insert(station);
        }else{
            //更新
            station.setUpdateTime(now);
            stationMapper.updateByPrimaryKey(station);
        }


    }
    //  查询唯一键
    private Station selectByUnique(String name) {
        StationExample example = new StationExample();
        example.createCriteria().andNameEqualTo(name);
        List<Station> list = stationMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(list)){
            return list.get(0);
        }else {
            return null;
        }
    }

    // 查询乘客列表
    public PageResp<StationQueryResp> queryList(StationQueryReq req){
        StationExample example = new StationExample();
        example.setOrderByClause("name_py asc");
        StationExample.Criteria criteria = example.createCriteria();
        //if(ObjUtil.isNotNull(req.getMemberId())){
            //criteria.andMemberIdEqualTo(req.getMemberId());
        //}

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Station> stationList = stationMapper.selectByExample(example);

        PageInfo<Station> pageInfo = new PageInfo<>(stationList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<StationQueryResp> list = BeanUtil.copyToList(stationList, StationQueryResp.class);

        PageResp<StationQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }
    //删除乘客
    public void delete(Long id){
        stationMapper.deleteByPrimaryKey(id);
    }

    // 查询车站列表全部
    public List<StationQueryResp> queryAll(){
        StationExample example = new StationExample();
        example.setOrderByClause("name_pinyin desc");
        List<Station> stationList = stationMapper.selectByExample(example);
        return BeanUtil.copyToList(stationList, StationQueryResp.class);
    }
}
